﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Protogamez
{
    /// <summary>
    /// Interaction logic for EditMode.xaml
    /// </summary>
    public partial class EditMode : Window
    {
        /// <summary>
        /// SET GLOBAL VARIABLE
        /// </summary>
        OleDbConnection conn = new OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;data source=" + AppDomain.CurrentDomain.BaseDirectory + "\\Resources\\gamesdb.mdb");
        string gamenamepass = "";

        public EditMode(string gamename)
        {
            InitializeComponent();
            gamenamepass = gamename;
        }

        /// <summary>
        /// SET REFERENCIAL VARIABLE
        /// </summary>
        string sqlresult1 = "";
        string sqlresult2 = "";
        string sqlresult3 = "";
        string sqlresult4 = "";
        string sqlresult5 = "";
        string sqlresult6 = "";
        string sqlresult7 = "";
        string sqlresult8 = "";
        string sqlresult9 = "";
        string sqlresult10 = "";
        string sqlresult11 = "";
        string sqlresult12 = "";

        string sqlcommand = "";

        string savepath = "";

        /// <summary>
        /// SET GLOBAL FUNCTION
        /// </summary>
        private void listbox_games_result(string sql)
        {
            OleDbCommand cmd = new OleDbCommand(sql, conn); conn.Open(); OleDbDataReader reader; reader = cmd.ExecuteReader();

            try
            {
                while (reader.Read())
                {
                    /// <summary>
                    ///SQL REFERENCE
                    ////// </summary>
                    //Game Name
                    sqlresult1 = reader.GetString(1).ToString();
                    //Save path
                    sqlresult2 = reader.GetString(2).ToString();
                    //Compatibility
                    sqlresult3 = reader.GetString(3).ToString();
                    //Backup State
                    sqlresult4 = reader.GetString(4).ToString();
                    //Status
                    sqlresult5 = reader.GetString(5).ToString();
                    //Type
                    sqlresult6 = reader.GetString(6).ToString();
                    //Note
                    sqlresult7 = reader.GetString(7).ToString();
                    //Plus
                    sqlresult8 = reader.GetString(8).ToString();
                    //Moin
                    sqlresult9 = reader.GetString(9).ToString();
                    //Description
                    sqlresult10 = reader.GetString(10).ToString();
                    //Path
                    sqlresult11 = reader.GetString(11).ToString();
                    //Last Played
                    sqlresult12 = reader.GetString(12).ToString();
                }
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }

            finally
            {
                reader.Close(); conn.Close();
            }
        }

        private void games_update(string sqlcommand)
        {
            conn.Open();
            OleDbCommand cmd = conn.CreateCommand();
            cmd.CommandText = sqlcommand;
            cmd.ExecuteNonQuery();
            conn.Close();
        }

        /// <summary>
        /// GLOBAL SOURCE CODE
        /// </summary>
        private void windows_edit_Loaded(object sender, RoutedEventArgs e)
        {
            string sql = "SELECT * FROM gamecentral WHERE gamename = '" + gamenamepass + "'";

            listbox_games_result(sql);

            label_gametitle.Content = sqlresult1;
            combobox_status.Text = sqlresult5;
            combobox_type.Text = sqlresult6;
            combobox_backup.Text = sqlresult4;
            combobox_compatibility.Text = sqlresult3;
            textbox_descriptionresult.Text = sqlresult10;
            combobox_noteresult.Text = sqlresult7;
            textbox_plusresult.Text = sqlresult8;
            textbox_minusresult.Text = sqlresult9;
            textbox_save.Text = sqlresult2;
            textbox_exe.Text = sqlresult11;

            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + gamenamepass + ".jpg"))
            {
                image_cover.Stretch = Stretch.Fill;
                image_cover.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + gamenamepass + ".jpg"));
            }
            else
            {
                image_cover.Stretch = Stretch.Uniform;
                image_cover.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"\Resources\protogamez_logo.png"));
            }
        }

        private void image_saaveexit_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            sqlcommand = @"UPDATE gamecentral SET pathtogame='" + textbox_exe.Text + "', savegamepath='" + textbox_save.Text + "', gamestatus='" + combobox_status.Text + "', gametype='" + combobox_type.Text + "', backupable='" + combobox_backup.Text + "', gamecompatibility='" + combobox_compatibility.Text + "', gamedescription='" + textbox_descriptionresult.Text + "', reviewnote='" + combobox_noteresult.Text + "', reviewplus='" + textbox_plusresult.Text + "', reviewmoin='" + textbox_minusresult.Text + "' WHERE gamename = '" + gamenamepass + "'";
            games_update(sqlcommand);
            this.Close();
        }

        private void image_cover_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var dialog = new System.Windows.Forms.OpenFileDialog();
            System.Windows.Forms.DialogResult result = dialog.ShowDialog();

            string imgpath = dialog.FileName;
            File.Copy(imgpath, AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + gamenamepass + ".jpg", true);

            image_cover.Stretch = Stretch.Fill;
            image_cover.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + gamenamepass + ".jpg"));
        }

        private void button_exe_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new System.Windows.Forms.OpenFileDialog();
            System.Windows.Forms.DialogResult result = dialog.ShowDialog();

            textbox_exe.Text = dialog.FileName;
        }

        private void button_path_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new System.Windows.Forms.FolderBrowserDialog();
            System.Windows.Forms.DialogResult result = dialog.ShowDialog();

            textbox_save.Text = dialog.SelectedPath;
        }
    }
}
